package com.bysj.sausys.controller.backend;

import com.bysj.sausys.beans.PageQuery;
import com.bysj.sausys.beans.PageResult;
import com.bysj.sausys.common.JsonData;
import com.bysj.sausys.common.UserConst;
import com.bysj.sausys.model.SysAssMember;
import com.bysj.sausys.service.ISysAssMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 社团会员Controller
 *
 * @author WangZK
 */
@Api(description = "社团会员信息接口")
@Controller
@RequestMapping("/sys/assmember")
public class SysAssMemberController {

    @Resource
    private ISysAssMemberService sysAssMemberService;

    /**
     * 添加社团会员
     *
     * @param sysAssMember
     * @param request
     * @return
     */
    @ApiOperation(value = "添加社团会员信息接口(社团管理员)")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public JsonData save(@ApiParam(value = "社团会员实体类", required = true) @RequestBody SysAssMember sysAssMember,
                         HttpServletRequest request) {
        // 从session中获取当前用户
        Map map = UserConst.getCurrentUser(request);
        sysAssMemberService.save(sysAssMember, map);
        return JsonData.success();
    }

    /**
     * 更新社团会员
     *
     * @param sysAssMember
     * @param request
     * @return
     */
    @ApiOperation(value = "更新社团会员信息接口（社团管理员）")
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    @ResponseBody
    public JsonData update(@ApiParam(value = "社团会员实体类", required = true) @RequestBody SysAssMember sysAssMember,
                           HttpServletRequest request) {
        // 从session中获取当前用户
        Map map = UserConst.getCurrentUser(request);
        sysAssMemberService.update(sysAssMember, map);
        return JsonData.success();
    }

    /**
     * 分页获取社团会员列表
     *
     * @param keyword
     * @param pageNo   页码
     * @param pageSize 每页显示条数
     * @return
     */
    @ApiOperation(value = "分页获取社团会员列表接口")
    @RequestMapping(value = "/getPage", method = RequestMethod.GET)
    @ResponseBody
    public JsonData getPage(@ApiParam(value = "社团Id") @RequestParam(required = false) Integer assId,
                            @ApiParam(value = "关键字") @RequestParam(required = false) String keyword,
                            @ApiParam(value = "页码") @RequestParam(required = false, defaultValue = "1") int pageNo,
                            @ApiParam(value = "每页显示条数") @RequestParam(required = false, defaultValue = "10") int pageSize) {
        PageResult result = sysAssMemberService.getPageByKeyword(assId, keyword, new PageQuery(pageNo, pageSize));
        return JsonData.success(result);
    }

    /**
     * 根据id获取社团会员信息
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id获取社团干部信息接口")
    @RequestMapping(value = "/getOne", method = RequestMethod.GET)
    @ResponseBody
    public JsonData getOne(@ApiParam(value = "社团会员id", required = true) @RequestParam Integer id) {
        SysAssMember sysAssMember = sysAssMemberService.findById(id);
        return JsonData.success(sysAssMember);
    }

    /**
     * 删除社团会员
     *
     * @param ids
     * @return
     */
    @ApiOperation(value = "删除社团会员接口（社团管理员）")
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonData delete(@ApiParam(value = "社团会员id数组", required = true) @RequestParam Integer[] ids) {
        sysAssMemberService.delete(ids);
        return JsonData.success();
    }

}
